package com.itheima.mythread.Demo7;


import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable implements Runnable {
    private int tickets = 30;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (tickets <= 0) {
                    break;
                } else {
                    Thread.sleep(300);
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + "正在售票,还剩" + tickets + "张票");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
        System.out.println("欢迎下次光临");
    }
}
